<template>
    <table border="1" align="center">
      <tr>
        <td colspan="6" align="center"><h1>新闻管理系统</h1>
          <router-link :to="{path:'/addNews'}">添加</router-link>
        </td>
      </tr>
      <tr><td>编号</td><td>标题</td><td>内容</td><td>作者</td><td>日期</td><td>操作</td></tr>
      <tr v-for="(news,index) in newsList" :key="index">
        <td>{{news.id}}</td>
        <td>{{news.title}}</td>
        <td>{{news.content}}</td>
        <td>{{news.author}}</td>
        <td>{{news.createDate}}</td>
        <td><button @click="deleteNews(news.id)">删除</button>
          <router-link :to="{path:'/updateNews',query:{news:news}}"><button>修改</button></router-link>
        </td>
      </tr>
    </table>
</template>

<script>
    export default {
        name: "showNews",
      data(){
          return {
            newsList:[]
          }
      },
      methods:{
        deleteNews(id){
          this.axios({
            url:"/news/"+id,
            method:"DELETE"
          }).then(resp=>{
            this.getNewsList();
          });
        },
        getNewsList(){
          this.axios({
            url:"/news",
            method:"GET"
          }).then(resp=>{
            this.newsList=resp.data;
          });
        }
      },
      created(){
          //发出axios请求获取newsList
        this.getNewsList();
      }
    }
</script>

<style scoped>

</style>
